import java.util.Date;  
  



import org.jnetpcap.Pcap;  
import org.jnetpcap.packet.PcapPacket;  
import org.jnetpcap.packet.PcapPacketHandler;  
import org.jnetpcap.protocol.tcpip.Http;
import org.jnetpcap.protocol.tcpip.Tcp;

public class SamplePcap {
    /** 
     * Main startup method 
     *  
     * @param args 
     *          ignored 
     */  
    public static void main(String[] args) {  
        /*************************************************************************** 
         * First we setup error buffer and name for our file 
         **************************************************************************/  
        final StringBuilder errbuf = new StringBuilder(); // For any error msgs  
        final String file = "e:\\test.pcap";  
  
        System.out.printf("Opening file for reading: %s%n", file);  
  
        /*************************************************************************** 
         * Second we open up the selected file using openOffline call 
         **************************************************************************/  
        Pcap pcap = Pcap.openOffline(file, errbuf);  
  
        if (pcap == null) {  
            System.err.printf("Error while opening device for capture: "  
                + errbuf.toString());  
            return;  
        }  
  
        /*************************************************************************** 
         * Third we create a packet handler which will receive packets from the 
         * libpcap loop. 
         **************************************************************************/  
        PcapPacketHandler<String> jpacketHandler = new PcapPacketHandler<String>() {  
        	final Tcp tcp = new Tcp();
        	final Http http = new Http(); 
            public void nextPacket(PcapPacket packet, String user) {  
  
                System.out.printf("Received at %s caplen=%-4d len=%-4d %s\n",  
                    new Date(packet.getCaptureHeader().timestampInMillis()),   
                    packet.getCaptureHeader().caplen(), // Length actually captured  
                    packet.getCaptureHeader().wirelen(), // Original length  
                    user // User supplied object  
                    ); 
                if (packet.hasHeader(Tcp.ID)) {
                	packet.getHeader(tcp);
                	System.out.printf("tcp.dst_port=%d%n", tcp.destination()); 
                	System.out.printf("tcp.src_port=%d%n", tcp.source());
                	System.out.printf("tcp.ack=%x%n", tcp.ack()); 
                	System.out.printf("tcp.seq=%x%n", tcp.seq()); 
                	System.out.printf("tcp.hlen=%x%n", tcp.hlen()); 
                	System.out.printf("tcp.reserved=%x%n", tcp.reserved());
                	System.out.printf("tcp.window=%x%n", tcp.window());
                	System.out.printf("tcp.checksum=%x%n", tcp.checksum());
                	System.out.printf("tcp.urgent=%x%n", tcp.urgent());
                	System.out.println("tcp.flags_ack="+tcp.flags_ACK());
                	System.out.println("tcp.flags_cwr="+tcp.flags_CWR());
                	System.out.println("tcp.offset="+tcp.getOffset());
                }
                if (packet.hasHeader(tcp)) {  
                    System.out.printf("tcp header::%s%n", tcp.toString());  
                }
                if (packet.hasHeader(tcp) && packet.hasHeader(http)) { 
                	System.out.printf("http header::%s%n", http); 
                }
                System.out.printf("frame #%d%n", packet.getFrameNumber()); 
            }  
        };  
  
        /*************************************************************************** 
         * Fourth we enter the loop and tell it to capture 10 packets. The loop 
         * method does a mapping of pcap.datalink() DLT value to JProtocol ID, which 
         * is needed by JScanner. The scanner scans the packet buffer and decodes 
         * the headers. The mapping is done automatically, although a variation on 
         * the loop method exists that allows the programmer to sepecify exactly 
         * which protocol ID to use as the data link type for this pcap interface. 
         **************************************************************************/  
        try {  
            pcap.loop(10, jpacketHandler, "jNetPcap rocks!");
//        	Worker worker1=new Worker();
//        	pcap.loop(10,worker1,"test");
        } finally {  
        /*************************************************************************** 
         * Last thing to do is close the pcap handle 
         **************************************************************************/  
           pcap.close();  
        }  
    }  
}
class Worker implements PcapPacketHandler{

	@Override
	public void nextPacket(PcapPacket pkt, Object obj) {
		System.out.println(pkt);
	}
	
}
